import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";
import cesium from "vite-plugin-cesium";
import dts from "vite-plugin-dts";

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  const isLibrary = mode === "library";

  return {
    plugins: [
      vue(),
      cesium(),
      dts({
        include: ["src/lib"], // 指定要生成类型声明的目录
        rollupTypes: true,
        tsconfigPath: "./tsconfig.app.json",
      }),
    ],
    server: {
      open: false,
      port: 2294,
    },
    resolve: {
      extensions: [".js", ".jsx", ".ts", ".tsx", ".json", ".vue"],
      mainFiles: ["index", "index.vue"],
      alias: [
        {
          find: "@",
          replacement: resolve(__dirname, "./src"),
        },
      ],
    },
    build: isLibrary
      ? {
        lib: {
          entry: resolve(__dirname, "src/lib/index.ts"),
          name: "SpaceAirGIS",
          fileName: (format) => `space-air-gis-cesium.${format}.js`,
          formats: ["es", "umd"],
        },
        outDir: "dist/lib",
        rollupOptions: {
          external: ["vue", "cesium", "naive-ui"],
          output: {
            globals: {
              vue: "Vue",
              cesium: "Cesium",
              "naive-ui": "NaiveUI",
            },
          },
        },
      }
      : {
        outDir: "dist",
        rollupOptions: {
          input: {
            main: resolve(__dirname, "index.html"),
          },
        },
      },
    optimizeDeps: {
      include: ["cesium", 'monaco-editor'],
    },

    worker: {
      format: 'es',
    },
  };
});
